#! /bin/sh

#set -x

# set parameters here
dmi=your_depth_section_here.su	  # depth migrated image
dz=3                              # depth sampling interval
dx=1                              # width sampling interval
perc=90
method=linear                     # interpolation method
parfile=unif2.par                 # output par file for unif2

# remove temporary files
echo "Removing temporary files, if they exist."
rm junk1.picks
rm junk1.picks.temp

echo
echo
echo
# Introductory information
echo  "Skip Introduction? (y/n) " | tr -d "\012" >/dev/tty
read response
case $response in
n*) # show introduction

echo
echo "         Horizon picker "
echo 
echo "Begin by setting necessary parameters in the shell script."
echo
echo "This is blind picking, you will not see a any indication of picks."
echo
echo "You will be prompted for the velocity in the top layer of the model"
echo
echo "The first horizon that you pick is the first reflector of the model"
echo
echo "Begin on the left side of the model. Place the point of the arrow "
echo "of the cursor echo on the point you want to pick, and press    s"
echo
echo "When you have picked the horizon from the left side of the model"
echo "to the right side, then echo type  q. A panel showing the model "
echo "will appear. If you type y at the prompt, then the script will"
echo "put the migrated image back up so you can pick the next horizon."
echo
echo "You will be prompted for the velocity in the layer immediately below"
echo "the current interface being drawn."
;;
*y) #continue

;;
esac

echo
echo
echo "Beginning the Horizon picking"
echo
pause 

vstring=
interf=0

echo 
echo
echo


# get value of ninf 
echo  "Input the number of interfaces ninf: " | tr -d "\012" >/dev/tty
read ninf
echo 
echo
echo

ok=false
while [ $ok = false ]
do

	echo  "Is ninf=$ninf correct? (y/n): " | tr -d "\012" >/dev/tty
	read response
	echo
	echo 
	echo
	case $response in
	n*) # ask for depth of model
		ok=false
		echo  "Input value of ninf: " | tr -d "\012" >/dev/tty
		read ninf
	;;
	y*) # continue
		ok=true
	;;
	esac
done



echo
echo
echo  "reading nz and nx values from $dmi"
echo
echo

# get value of nz 
nz=`suwind count=1  < $dmi | sugethw ns output=geom`
ok=false
while [ $ok = false ]
do
	echo  "Assuming model depth in samples is nz=$nz. Is that ok? (y/n) " | tr -d "\012" >/dev/tty
	read response
	case $response in
	n*) # ask for depth of model
		ok=false
		echo  "Input depth of model nz: " | tr -d "\012" >/dev/tty
		read nz
	;;
	y*) # continue
		ok=true
	;;
	esac
done
# get value of dz 
dz=`suwind count=1  < $dmi | sugethw d1 output=geom`
ok=false
while [ $ok = false ]
do
	echo  "Assuming model depth increment dz=$dz. Is that ok? (y/n) " | tr -d "\012" >/dev/tty
	read response
	case $response in
	n*) # ask for depth of model
		ok=false
		echo  "Input depth increment of model dz: " | tr -d "\012" >/dev/tty
		read dz
	;;
	y*) # continue
		ok=true
	;;
	esac
done

# get value of nx
nx=`sucountkey key=tracl verbose=0 < $dmi | awk '{ print $2  }'`
ok=false
while [ $ok = false ]
do
	echo  "Assuming model width nx=$nx. Is this value ok? (y/n) " | tr -d "\012" >/dev/tty
	read response
	case $response in
	n*) # ask for width of model
		ok=false
		echo  "Input width of model nx: " | tr -d "\012" >/dev/tty
		read nx
	;;
	y*) # continue
		ok=true
	;;
	esac
done
 
# get value of dx
dx=`suwind count=1  < $dmi | sugethw d2 output=geom`
ok=false
while [ $ok = false ]
do
	echo  "Assuming model horizontal increment dx=$dx. (This should be the value of  dxcdp.) Is this value correct? (y/n) " | tr -d "\012" >/dev/tty
	read response
	case $response in
	n*) # ask for width increment of model
		ok=false
		echo  "Input horizontal increment of model dx: " | tr -d "\012" >/dev/tty
		read dx
	;;
	y*) # continue
		ok=true
	;;
	esac

done

# calculate distance across model
xdist=`bc -l <<END
	$dx * $nx
END`


# input first velocity
ok=false
while [ $ok = false ]
do
	echo "Type velocity in top layer: "| tr -d "\012" >/dev/tty
	read velocity

	echo

	echo "Is velocity=$velocity correct? (y/n)" | tr -d "\012" >/dev/tty
	read response

	case $response in
	n*) # is false
		ok=false
	;;
	y*) # is true
		ok=true
	;;
	esac
done

vstring=$velocity

# input first dv/dz 
ok=false
while [ $ok = false ]
do
	echo "Type dv/dz in top layer: "| tr -d "\012" >/dev/tty
	read dvdz

	echo

	echo "Is dv/dz=$dvdz correct? (y/n)" | tr -d "\012" >/dev/tty
	read response

	case $response in
	n*) # is false
		ok=false
	;;
	y*) # is true
		ok=true
	;;
	esac
done
dvdzstring=$dvdz

# input first dv/dx 
ok=false
while [ $ok = false ]
do
	echo "Type dv/dx in top layer: "| tr -d "\012" >/dev/tty
	read dvdx

	echo

	echo "Is dv/dx=$dvdx correct? (y/n)" | tr -d "\012" >/dev/tty
	read response

	case $response in
	n*) # is false
		ok=false
	;;
	y*) # is true
		ok=true
	;;
	esac
done
dvdxstring=$dvdx



# loop over interfaces
while [ $interf -lt $ninf ]
do
	ok=false

	if [ $interf -eq 0 ]
	then
		echo "0  0 " > junk1.picks
		echo "$xdist 0 " >> junk1.picks
		echo "1  -999999" >> junk1.picks
	fi

	# update interf for next interface
	interf=` expr $interf + 1 `
	maxinterf=` expr $ninf `


	# begin horizon picking
	while [ $ok = false ]
	do
		echo 
		echo "Picking horizon $interf of $maxinterf "
		echo 
	
		# if junk1.picks exists, save in a temporary file
		if [ -f junk1.picks ]
		then
			cp junk1.picks junk1.picks.temp
		fi 


		# view migrated image
		suximage < $dmi mpicks=junk.picks perc=$perc d1=$dz d2=$dx key=tracl

		cp junk.picks junk.picks.temp
		cat junk.picks.temp |
		awk '{ print $2" "$1 }' >> junk1.picks.temp

		echo "1  -999999" >> junk1.picks.temp

		# get the z0 and  x0 values
		LIST=`cat junk.picks.temp | awk '{ print $1 }'`

		z0=`echo $LIST | awk '{ print $1 }'`
		x0=0
		if [ $interf -eq 1 ]
		then
			z0string=0,$z0
			x0string=0,$x0
			z0temp=$z0string
			x0temp=$x0string
		else
			z0temp=$z0string
			x0temp=$x0string
			z0string=$z0string,$z0
			x0string=$x0string,$x0
		fi

		# check to see if velocities are ok
		ok=false
		while [ $ok = false ]
		do
			echo "Type velocity in layer below interface: "| tr -d "\012" >/dev/tty
			read velocity

			echo

			echo "Is velocity=$velocity correct? (y/n)" | tr -d "\012" >/dev/tty
			read response

			case $response in
			n*) # is false
				ok=false
			;;
			y*) # is true
				ok=true
			;;
			esac
		done
		tmpvstring=$vstring
		vstring=$vstring,$velocity

		echo velocity=$vstring

		# get dv/dz
		ok=false
		while [ $ok = false ]
		do
			echo "Type dv/dz in layer below interface: "| tr -d "\012" >/dev/tty
			read dvdz

			echo

			echo "Is dv/dz=$dvdz correct? (y/n)" | tr -d "\012" >/dev/tty
			read response

			case $response in
			n*) # is false
				ok=false
			;;
			y*) # is true
				ok=true
			;;
			esac
		done
		tmpdvdzstring=$dvdzstring
		dvdzstring=$dvdzstring,$dvdz

		echo dvdz=$dvdzstring

		# get dv/dx
		ok=false
		while [ $ok = false ]
		do
			echo "Type dv/dx in layer below interface: "| tr -d "\012" >/dev/tty
			read dvdx

			echo

			echo "Is dv/dx=$dvdx correct? (y/n)" | tr -d "\012" >/dev/tty
			read response

			case $response in
			n*) # is false
				ok=false
			;;
			y*) # is true
				ok=true
			;;
			esac
		done
		tmpdvdxstring=$dvdxstring
		dvdxstring=$dvdxstring,$dvdx

		echo dvdx=$dvdxstring

		# view picks
		echo
		echo "click on image and type: q to close ximage window "
		echo

		unif2 < junk1.picks.temp nz=$nz dz=$dz nx=$nx dx=$dx \
				ninf=$interf v00=$vstring dvdz=$dvdzstring \
				dvdx=$dvdxstring  x0=$x0string z0=$z0string |
				ximage n1=$nz n2=$nx d1=$dz d2=$dx \
				cmap=hsv0 verbose=0 legend=1

		# check to see if picks are ok
		echo  "Picks Ok? (y/n) " | tr -d "\012"
		read response

		case $response in
		y*) ok=true
			cp junk1.picks.temp junk1.picks

		;;
		n*) # not ok, redo picking
			ok=false
			# go back to previous picked values
			interf=` expr $interf `
			dvdxstring=$tmpdvdxstring
			dvdzstring=$tmpdvdzstring
			vstring=$tmpvstring
			z0string=$z0temp
			x0string=$x0temp
			;;
			esac
	
	done
	

done
#

ninf=$interf

cp junk1.picks temp.ascii

rm $parfile
# build par file
echo " nz=$nz dz=$dz nx=$nx dx=$dx ninf=$ninf " > $parfile
echo " n1=$nz d1=$dz n2=$nx d2=$dx " >> $parfile
echo " v00=$vstring dvdz=$dvdzstring " >> $parfile
echo " dvdx=$dvdxstring  x0=$x0string z0=$z0string" >> $parfile

tempninf=`expr $ninf + 1`

smoothint2 < temp.ascii r1=100 ninf=$tempninf > unif2.ascii 

unif2 < unif2.ascii nz=$nz dz=$dz nx=$nx dx=$dx \
	ninf=$ninf v00=$vstring dvdz=$dvdzstring \
	dvdx=$dvdxstring  x0=$x0string z0=$z0string |
	smooth2 n1=$nz n2=$nx r1=10 r2=10 > junk.bin

cp junk.bin hnewvelzx.bin

transp n1=$nz < hnewvelzx.bin > hnewvelxz.bin

echo "Outputting parameter file for unif2:   $parfile"
echo "Outputting input model file for unif2:  unif2.ascii"
echo "Outputting v(z,x) velocity file: hnewvelzx.bin   "
echo "Outputting v(x,z) velocity file: hnewvelxz.bin   "
echo
echo "You may hand edit the files $parfile and unif2.ascii"
echo "and run the shell script Unif2.sh to make updated"
echo "v(z,x) and v(x,z) files."

exit 0
